'use strict';

var lambApp = angular.module('lambApp', [
  'ui.router',
  "ui.bootstrap",
  "oc.lazyLoad",
  "MetronicApp",
  "translate",
  "common",
  'home',
  'menu'
]);
var basePath = (function () {
  var url = window.location + "";
  var h = url.split("//");
  var x = h[1].split("/");
  return h[0] + "//" + window.location.host + "/" + x[1] + "/";
})();
basePath = "http://localhost:8080/lamb/";
//basePath = "./data/";

lambApp
  //路由配置
  .config(['$stateProvider', '$urlRouterProvider', function ($stateProvider, $urlRouterProvider) {
    $urlRouterProvider.otherwise("/");

    $stateProvider
      .state('login', {
        url: '/login',
        templateUrl: 'views/login.html',
        controller: 'loginCtrl',
        resolve: {
          deps: ['$ocLazyLoad', function ($ocLazyLoad) {
            return $ocLazyLoad.load(['assets/admin/pages/css/login-soft.css', '../bower_components/jquery-backstretch/jquery.backstretch.min.js'])
          }]
        }
      })
      .state('index', {
        abstract: true,
        url: '/',
        templateUrl: 'views/home.html'
      })
      .state('index.dashboard', {
        url: '',
        templateUrl: 'views/dashboard.html'
      })
      .state('index.user', {
        url: 'user/userList/:menuId',
        templateUrl: 'views/user/userList.html',
        controller: "userListCtrl",
        resolve: {
          deps: ['$ocLazyLoad', function ($ocLazyLoad) {
            return $ocLazyLoad.load(['scripts/modules/user.js'])
          }]
        }
      })
      .state("index.user.userAdd", {
        url: 'user/userAdd',
        templateUrl: 'views/user/userAdd.html'
      })
  }])
  //$http设置
  .config(['$httpProvider', function ($httpProvider) {
    //配置跨域使用cookie
    $httpProvider.defaults.withCredentials = true;
  }]);

/* Init global settings and run the app */
lambApp.run(["$rootScope", "settings", "$state", "loginService", "$http", "i18n",
  function ($rootScope, settings, $state, loginService, $http, i18n) {
    //国际化配置
    i18n.init("scripts/languages/", "en");

    //路由切换时验证登录状态
    $rootScope.$on('$stateChangeStart', function (event, toState, toParams, fromState, fromParams) {
      //若没有登录信息，则到服务器查询（保证刷新页面后不用重新登录）
      if (!loginService.isLogin && toState.name != "login") {
        event.preventDefault();
        loginService.verifyLogin(toState,toParams)
      }
    });
    $rootScope.$state = $state; // state to be accessed from view
  }]);
